<?php

class XQL_Simple_DeleteQuery extends XQL_SimpleQuery
{
	protected $executionMode = XQL_Query::MODE_EXEC;

	public function __construct($querySetName, $connectionAlias='default')
	{
		parent::__construct($querySetName, $connectionAlias);

		if (!$this->isQueryLoaded())
		{
			$this->setAction(
				XQL_Delete::create()->
				join(
					XQL_From::create()->set(
						XQL_Table::create()->set(
							XQL_Item::create()->set(':table')
						)
					)
				)->
				join(
					XQL_Where::create()->set(
						XQL_Item::create()->set(':where')
					)
				)
			);
		}
	}

	/**
	 * Visszad egy példányt az osztályból.
	 *
	 * @return XQL_Simple_DeleteQuery
	 */
	static public function create($querySetName, $connectionAlias='default')
	{
		return new XQL_Simple_DeleteQuery($querySetName, $connectionAlias);
	} // static method

	protected function onBeforeExecute($args)
	{
		$wherePartString = '';
		$keys = (!empty($this->keys)? implode(', ', XQL_Util::argumentToFields($this->keys)) : '*');

		if (count($args) > 0)
		{
			if ($args[0] instanceof XQL_Where)
			{
				$this->setAction(
					XQL_Delete::create()->
					join(
						XQL_From::create()->set(
							XQL_Table::create()->set(
								XQL_Item::create()->set(':table')
							)
						)
					)->
					join(
						$args[0]
					)
				);
				$this->query = (string)$this->getAction();
			}
			else
			{
				if ($this->hasTransformableData())
				{
					$args[0] = $this->dataTransformation->encodeRecord($args[0]);
				}
				$wherePartString = XQL_Util::argumentToFilter($args[0]);
			}
		}
		else
		{
			//throw new eSimpleDeleteQuery(eSimpleDeleteQuery::NOT_SAFE);
		}

		$rep = array(
			':table'	=> (string)$this->table,
			':where'	=> (string)$wherePartString
		);
		$this->query = str_replace(array_keys($rep), array_values($rep), $this->query);

		return $this;
	}
}

?>